/**
 * The number 3797 has an interesting property. 
 * Being prime itself, it is possible to continuously remove digits from left to right, 
 * and remain prime at each stage: 3797, 797, 97, and 7. 
 * Similarly we can work from right to left: 3797, 379, 37, and 3.
 * Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
 * NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
 * @author TrinhNX
 * @start_date	: Apr 26, 2015
 * @end_date 	: First time
 */
public class Euler037 {

	public static void main(String[] args) {
		final long start = System.currentTimeMillis();
		int sum = 0;
		int counter = 0;
		int start_index = 23;
		System.out.println("Start");
		boolean check;
		int m, n, base10;
		do {
			if (Common.isPrime(start_index)) {
				check = true;
				m = start_index;
				n = 10;
				base10 = Common.power10(Integer.toString(start_index).length() - 1);
				while (base10 > 1) {
					n = start_index % base10;
					m = start_index / base10;
					if (!Common.isPrime(m) || !Common.isPrime(n)) {
						check = false;
						break;
					}
					base10 /= 10;
				}
				if (check) {
					sum += start_index;
					counter++;
				}
			}
			start_index += 2;
		} while (counter < 11);
		System.out.println(sum);
		System.out.println("End after" + (System.currentTimeMillis() - start));
	}
}
